package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChangePlaceActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView stuTel;
    private Spinner mSpinner;
    private EditText stuBed;
    private EditText stuReason;
    private Button submitButton;
    private String getTypeName, getUserAccount;
    private LoginResponse mLoginResponse;
    private String[] typeName = {"服从", "不服从"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_place);
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");

        initViews();
        ArrayAdapter<String> type_adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item,typeName);
        mSpinner.setAdapter(type_adapter);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                getTypeName = typeName[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initViews() {
        stuTel = findViewById(R.id.stu_phone);
        mSpinner = findViewById(R.id.allow_change);
        stuBed = findViewById(R.id.stu_bed);
        stuReason = findViewById(R.id.stu_reason);
        submitButton = findViewById(R.id.submit_button);
        submitButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                if (stuTel.getText().toString().length() != 0 && stuBed.getText().toString().length() != 0 && stuReason.getText().toString().length() != 0 && getTypeName.length() != 0) {
                    postChangePlaceInfo(getTypeName, stuBed.getText().toString(), stuReason.getText().toString(), stuTel.getText().toString());
                } else {
                    Toast.makeText(this, "请检查信息输入是否正确！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void postChangePlaceInfo(String allowChange, String wantBed, String wantReason, String userTel) {
        RetrofitDevelop.getTestService()
                .addPlaceChangeInfo(allowChange, wantBed, wantReason, userTel, getUserAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(ChangePlaceActivity.this, "信息提交成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(ChangePlaceActivity.this, "信息提交失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(ChangePlaceActivity.this, "网络错误！请稍后再试", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }
}
